#!/usr/bin/env bash

# Runs a browser bundle that can run TOR, I2P, XD torrenting client and shut
# everything down automatically when browser closes.
# License: CC0

# Usage:
# - Keep this script in a directory
# - Make sure to have firefox, tor, i2pd, XD installed or built from source
#   Instructions: linux/i2p-browser-bundle/README.md
# - Launch it, install FoxyProxy plugin, import the
#   `FoxyProxy Standard presets.json` in this directory. Enable the rules by
#   clicking the toolbar icon -> Use Enabled Proxies By Patterns and Order
# - Don't forget to apply settings changes and check suggested addons from:
#   - https://privacytools.io/browsers/#webrtc
#   - https://privacytools.io/browsers/#addons
#   - https://privacytools.io/browsers/#about_config

cd `dirname $0`
echo "== Running I2P Bundle from $(pwd)..."

# Ports
# If you change these you may have to change them on FoxyProxy later on (check
# both edit and patterns)
tor_socks_port=9450
i2pd_http_port=4455
i2pd_socks_port=4449
i2pd_webconsole_port=6060
xd_webui_port=1776


echo "== Attempting to run the browser..."
echo "Once it runs you will be able to access:"
echo "- i2pd panel: http://127.0.0.1:$i2pd_webconsole_port/"
echo "- XD webui: http://127.0.0.1:$xd_webui_port/"
echo "- Be sure to have FoxyProxy addon configured with the accompanied json file to access things properly"


# Runs i2pd
ulimit -n 4096
if [ -f i2pd/i2pd ]; then
	# Setting data dir and ports as we have in FoxyProxy.
	# --loglevel is set to none because i2pd logs never gave me any clue to
	# what's wrong. Remove it to get logs back.
	# SAM is enabled for XD to work.
	i2pd/i2pd --datadir i2pd-data --loglevel=none --http.port=$i2pd_webconsole_port --httpproxy.enabled=1 --httpproxy.port=$i2pd_http_port --socksproxy.port=$i2pd_socks_port --sam.enabled=1 &
else
	i2pd --datadir i2pd-data --loglevel=none --http.port=$i2pd_webconsole_port --httpproxy.enabled=1 --httpproxy.port=$i2pd_http_port --socksproxy.port=$i2pd_socks_port --sam.enabled=1 &
fi
i2pd_pid=$!

# Runs XD BitTorrent client
[ ! -d XD-data ] && mkdir XD-data
cd XD-data
if [ -f ../XD/XD ]; then
	../XD/XD torrents.ini &
else
	XD torrents.ini &
fi
xd_pid=$!
cd ..

# Runs tor
mkdir tor-data
if [ -f tor/tor ]; then
	tor/tor --SocksPort $tor_socks_port --DataDirectory tor-data --User '' &
else
	tor --SocksPort $tor_socks_port --DataDirectory tor-data --User '' &
fi
tor_pid=$!

# Quick and simple way to kill a process by its pid number
# $1: pid
# $2: binary name
_killpid () {
	# Send QUIT signal (-3) first
	kill -3 $1 || echo "$2, pid $1 not running, so skipping kill call"
	# Give it 7 seconds then silently hard kill the pid if still running
	sleep 7 && kill -9 $1 &>/dev/null
}


# Change the command to anything you wish, but keep the ";" at the end because
# it helps to shut everything down once the browser closes.
firefox --no-remote --profile "$PWD/tor-firefox-profile" "http://127.0.0.1:$i2pd_webconsole_port" "http://127.0.0.1:$xd_webui_port";
echo "== Killing XD, i2pd and tor...";
_killpid "$xd_pid" 'XD' &
_killpid "$i2pd_pid" 'i2pd' &
_killpid "$tor_pid" 'tor' &
